/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BussinessObject.NhaphangBO;
import BussinessObject.SanphamBO;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ngoctoan
 */
public class NhaphangGUI extends javax.swing.JInternalFrame {

    /**
     * Creates new form NhaphangGUI
     */
    private DefaultTableModel model = new DefaultTableModel();
    private ArrayList<SanphamBO> list = new ArrayList<SanphamBO>();
    private int seletedIndex = 0;

    public NhaphangGUI() {
        initComponents();
        LoadData();

        model.setColumnIdentifiers(new String[]{"Mã sp", "Tên sp", "Đơn vị tính", "Số lượng", "Đơn giá nhập", "Đơn giá bán", "Thành tiền"});
        tbSanpham.setModel(model);
        TaoMaPN();

        txtMapn.setEditable(false);
        txtNgaylap.setEditable(false);
        txtDvt.setEditable(false);
    }

    private void TaoMaPN() {
        NhaphangBO nhBO = new NhaphangBO();
        ResultSet rs = nhBO.LayIDpn();
        int returnId = 0;
        try {
            while (rs.next()) {
                returnId = Integer.parseInt(rs.getString(1)) + 1;
            }
        } catch (SQLException ex) {
            Logger.getLogger(NhaphangGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        txtMapn.setText("PN" + String.format("%05d", returnId));
        String timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(Calendar.getInstance().getTime());
        txtNgaylap.setText(timeStamp);
    }

    private void TinhTongTien() {
        double tongTien = 0;
        for (Iterator<SanphamBO> it = list.iterator(); it.hasNext();) {
            SanphamBO object = it.next();
//            System.out.print(object.Soluong);
//            System.out.print(object.Giaban);
//            System.out.println();
            tongTien += object.Soluong * object.Gianhap;
        }
        txtTongtien.setText(String.valueOf(tongTien));
    }

    private void LoadData() {
        try {
            NhaphangBO nhBO = new NhaphangBO();
            ResultSet rs = nhBO.LayMasp();
            cboMasp.removeAllItems();
            while (rs.next()) {
                cboMasp.addItem(rs.getString(2));
            }
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jErrorProvider = new javax.swing.JErrorProvider();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTensp = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cboMasp = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        txtSoluong = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtGianhap = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtGiaban = new javax.swing.JTextField();
        btnThem = new javax.swing.JButton();
        btnLuu = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        txtID_Sanpham = new javax.swing.JTextField();
        txtDvt = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbSanpham = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtMapn = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNgaylap = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTongtien = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtGhichu = new javax.swing.JTextArea();
        btnNhaphang = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jTabbedPane3 = new javax.swing.JTabbedPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableDanhmuc1 = new javax.swing.JTable();

        setClosable(true);
        setTitle("QUẢN LÝ NHẬP HÀNG");
        setPreferredSize(new java.awt.Dimension(900, 637));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Thông tin sản phẩm"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Mã sản phẩm");

        txtTensp.setEditable(false);
        txtTensp.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setText("Tên sản phẩm");

        cboMasp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboMasp.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboMaspItemStateChanged(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setText("Số lượng");

        txtSoluong.setPreferredSize(new java.awt.Dimension(10, 25));
        txtSoluong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSoluongKeyTyped(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel8.setText("Đơn giá bán");

        txtGianhap.setPreferredSize(new java.awt.Dimension(10, 25));
        txtGianhap.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtGianhapKeyTyped(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel9.setText("Đơn giá nhập");

        txtGiaban.setPreferredSize(new java.awt.Dimension(10, 25));
        txtGiaban.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtGiabanKeyTyped(evt);
            }
        });

        btnThem.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\add.png")); // NOI18N
        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnLuu.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\save.png")); // NOI18N
        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnXoa.setIcon(new javax.swing.ImageIcon("D:\\supermarket-management-system\\src\\resource\\delete.png")); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        txtID_Sanpham.setPreferredSize(new java.awt.Dimension(10, 25));

        txtDvt.setPreferredSize(new java.awt.Dimension(10, 25));
        txtDvt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtDvtKeyTyped(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel10.setText("ĐVT");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtTensp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtGianhap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cboMasp, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtGiaban, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtSoluong, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtDvt, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(txtID_Sanpham, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLuu, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboMasp, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTensp, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtSoluong, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtDvt, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtGianhap, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtGiaban, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLuu, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtID_Sanpham, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Chi tiết phiếu nhập"));

        tbSanpham.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tbSanpham.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbSanpham.setRowHeight(25);
        tbSanpham.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbSanphamMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbSanpham);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Mã phiếu nhập");

        txtMapn.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setText("Ngày lập");

        txtNgaylap.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Tổng tiền");

        txtTongtien.setPreferredSize(new java.awt.Dimension(10, 25));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Ghi chú");

        txtGhichu.setColumns(20);
        txtGhichu.setRows(5);
        jScrollPane2.setViewportView(txtGhichu);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTongtien, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMapn, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(txtNgaylap, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(23, 23, 23)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtMapn, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNgaylap, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(13, 13, 13)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTongtien, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnNhaphang.setText("Lập phiếu nhập hàng");
        btnNhaphang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNhaphangActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addContainerGap())
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 185, Short.MAX_VALUE)
                                .addComponent(btnNhaphang, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(45, 45, 45))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnNhaphang, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 47, Short.MAX_VALUE))))
        );

        jTabbedPane2.addTab("Thông tin phiếu nhập hàng", jPanel1);

        jTabbedPane1.addTab("Lập phiếu nhập hàng", jTabbedPane2);

        tableDanhmuc1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        tableDanhmuc1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableDanhmuc1.setRowHeight(25);
        jScrollPane3.setViewportView(tableDanhmuc1);

        jTabbedPane3.addTab("Phiếu nhập hàng", jScrollPane3);

        jTabbedPane1.addTab("Danh sách phiếu nhập hàng", jTabbedPane3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cboMaspItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboMaspItemStateChanged
        // TODO add your handling code here:
        // System.out.println(cboMasp.getSelectedItem());
        jErrorProvider.clear();
        NhaphangBO nhBO = new NhaphangBO();
        ResultSet rs = nhBO.LayCtsp(cboMasp.getSelectedItem());
        try {
            while (rs.next()) {
                txtID_Sanpham.setText(rs.getString("ID_Sanpham"));
                txtTensp.setText(rs.getString("Tensp"));
                txtDvt.setText(rs.getString("Donvitinh"));
                txtSoluong.setText("");
                txtGianhap.setText("");
                txtGiaban.setText("");
            }
        } catch (SQLException ex) {
            Logger.getLogger(NhaphangGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cboMaspItemStateChanged

    public void actionPerformed(ActionEvent e, ArrayList<SanphamBO> list) {

        model.setRowCount(list.size());
        int row = 0;
        for (SanphamBO data : list) {
            model.setValueAt(data.Masp, row, 0);
            model.setValueAt(data.Tensp, row, 1);
            model.setValueAt(data.Donvitinh, row, 2);
            model.setValueAt(data.Soluong, row, 3);
            model.setValueAt(data.Gianhap, row, 4);
            model.setValueAt(data.Giaban, row, 5);
            model.setValueAt(data.Gianhap * data.Soluong, row, 6);
            row++;
        }
        tbSanpham.setModel(model);
    }

    private boolean VadidateCtpn() {
        if (txtSoluong.getText().trim().equals("")) {
            jErrorProvider.setError(txtSoluong, "Số lượng bắt buộc");
            return false;
        }
        if (txtGianhap.getText().trim().equals("")) {
            jErrorProvider.setError(txtGianhap, "Đơn giá bắc buộc");
            return false;
        }
        return true;
    }

    private boolean VadidatePhieunhap() {
        if (tbSanpham.getRowCount() == 0) {
            jErrorProvider.setError(btnNhaphang, "Phải nhập chi tiết phiếu nhập");
            return false;
        }
        return true;
    }

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        // TODO add your handling code here:

        jErrorProvider.clear();
        if (VadidateCtpn()) {
            int ID_Sanpham = Integer.parseInt(txtID_Sanpham.getText());
            String Masp = (String) cboMasp.getSelectedItem();
            String Tensp = txtTensp.getText();
            String Donvitinh = txtDvt.getText();
            double Gianhap = Double.parseDouble(txtGianhap.getText().trim());
            double Giaban = Double.parseDouble(txtGiaban.getText().trim());
            int Soluong = Integer.parseInt(txtSoluong.getText().trim());


            for (Iterator<SanphamBO> it = list.iterator(); it.hasNext();) {
                SanphamBO object = it.next();
                if (ID_Sanpham == object.ID_Sanpham) {
                    list.get(seletedIndex).Soluong = Soluong;
                    list.get(seletedIndex).Giaban = Giaban;
                    actionPerformed(evt, list);
                    cboMasp.setSelectedIndex(0);
                    TinhTongTien();
                    return;
                }
            }

            list.add(new SanphamBO(ID_Sanpham, Masp, Tensp, Donvitinh, Gianhap, Giaban, Soluong));
            actionPerformed(evt, list);
            TinhTongTien();
        }
    }//GEN-LAST:event_btnLuuActionPerformed

    private void txtSoluongKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSoluongKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_ENTER) || (c == KeyEvent.VK_PERIOD))) {
            evt.consume();
        }
    }//GEN-LAST:event_txtSoluongKeyTyped

    private void txtGianhapKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtGianhapKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if (!((c >= '0') && (c <= '9') || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_ENTER) || (c == KeyEvent.VK_PERIOD))) {
            evt.consume();
        }
    }//GEN-LAST:event_txtGianhapKeyTyped

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        cboMasp.setSelectedIndex(0);
        txtGianhap.setText("");
        txtGiaban.setText("");
        txtSoluong.setText("");
    }//GEN-LAST:event_btnThemActionPerformed

    private void tbSanphamMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbSanphamMouseClicked
        // TODO add your handling code here:
        cboMasp.setSelectedItem(tbSanpham.getValueAt(tbSanpham.getSelectedRow(), 0).toString());
        txtSoluong.setText(String.format("%03d", tbSanpham.getValueAt(tbSanpham.getSelectedRow(), 2)));
        txtGianhap.setText(String.format("#", tbSanpham.getValueAt(tbSanpham.getSelectedRow(), 3)));
        seletedIndex = tbSanpham.getSelectedRow();
    }//GEN-LAST:event_tbSanphamMouseClicked

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int dialogResult = JOptionPane.showConfirmDialog(this, "Bạn có chắc chắn xóa chi tiết phiếu nhập?", "Xóa chi tiết phiếu nhập", dialogButton);
        if (dialogResult == 0) {
            list.remove(seletedIndex);
            actionPerformed(evt, list);
            TinhTongTien();
            //JOptionPane.showMessageDialog(null, "Xóa thành công chi tiết phiếu nhập!", "Xóa chi tiết phiếu nhập", JOptionPane.NO_OPTION);
        }

    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnNhaphangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNhaphangActionPerformed
        // TODO add your handling code here:
        if (VadidatePhieunhap()) {
            NhaphangBO nhBO = new NhaphangBO();
            String Maphieu = txtMapn.getText().trim();
            Boolean Loai = true;
            int IdNguoidung = 1;
            Double Tongtien = Double.parseDouble(txtTongtien.getText().trim());
            String Ghichu = txtGhichu.getText().trim();
            int Id_Nhapxuat = nhBO.ThemPhieuNhap(Maphieu, Loai, IdNguoidung, Tongtien, Ghichu);

            for (SanphamBO data : list) {
                int Id_Sanpham = data.ID_Sanpham;
                int Soluong = data.Soluong;
                double Gianhap = data.Gianhap;
                double Giaban = data.Giaban;
                double Thanhtien = data.Soluong * data.Gianhap;
                nhBO.ThemCtpn(Id_Nhapxuat, Id_Sanpham, Soluong, Gianhap, Thanhtien);
                nhBO.CapNhatSPNhapHang(Id_Sanpham, Soluong, Giaban);
            }

            list.clear();
            actionPerformed(evt, list);
            TinhTongTien();
            TaoMaPN();
        }
    }//GEN-LAST:event_btnNhaphangActionPerformed

    private void txtGiabanKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtGiabanKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtGiabanKeyTyped

    private void txtDvtKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDvtKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDvtKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnNhaphang;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cboMasp;
    private javax.swing.JErrorProvider jErrorProvider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTabbedPane jTabbedPane3;
    private javax.swing.JTable tableDanhmuc1;
    private javax.swing.JTable tbSanpham;
    private javax.swing.JTextField txtDvt;
    private javax.swing.JTextArea txtGhichu;
    private javax.swing.JTextField txtGiaban;
    private javax.swing.JTextField txtGianhap;
    private javax.swing.JTextField txtID_Sanpham;
    private javax.swing.JTextField txtMapn;
    private javax.swing.JTextField txtNgaylap;
    private javax.swing.JTextField txtSoluong;
    private javax.swing.JTextField txtTensp;
    private javax.swing.JTextField txtTongtien;
    // End of variables declaration//GEN-END:variables
}
